import {getStorageUserInfo} from './tools';

function httpStatusCodeToMessage(code) {

  const msgs = {
    400: '语义有误，当前请求无法被服务器理解,请求参数有误',
    401: '验证已失效,请请重新登录',
    429: '网络请求次数太频繁了',
    404: '请求失败，请求所希望得到的资源未被在服务器上发现',
    405: '请求行中指定的请求方法不能被用于请求相应的资源',
    412: '图像验证码错误,服务器在验证在请求的头字段中给出先决条件时，没能满足其中的一个或多个',
    500: '服务器错误',
    777: '其他错误',
  };
  let title = '';
  if (code in msgs) {
    title = code + ':' + msgs[code];
  } else {
    title = code + ':' + msgs[777];
  }

  return title;
}

export default {
  install(Vue, options) {
    Vue.prototype.ezNotifyAxiosThen = function(resBody) {
      let type = 'warning';
      let title = '提示';
      let message = '';
      //console.log(resBody);
      if (typeof(resBody.code) != 'undefined') {
        type = resBody.code > 0 ? 'success' : 'warning';
        title = resBody.code > 0 ? '成功' : '提示';
        message = resBody.code > 0 ? '' : resBody.msg;
        //console.log(message);
      }
      if (typeof(resBody.status) != 'undefined') {
        type = resBody.status > 0 ? 'success' : 'warning';
        title = resBody.status > 0 ? '成功' : '提示';
        message = resBody.code > 0 ? '' : resBody.msg;
        //console.log(message);
      }
      this.$notify({title, message, type});
    };
    Vue.prototype.ezNotifyAxiosCatch = function(error) {
      let code = 500;
      try {
        code = error.response.status;
      } catch (e) {
        code = 500;
      }
      this.$notify({
        title: code,
        message: httpStatusCodeToMessage(code),
        type: 'error',
      });
    };

    //事件监听bus
    Vue.prototype.$bus = new Vue();

    //是否是系统管理员
    Vue.prototype.isUserSystemAdmin = function() {
      try {
        return (getStorageUserInfo().role_id === 1);
      } catch (e) {
        return false;
      }
    }();
    Vue.prototype.isUserSystemAccount = function() {
      try {
        return (getStorageUserInfo().role_id === 4);
      } catch (e) {
        return false;
      }
    }();
    Vue.prototype.isUserSystemRisk = function() {
      try {
        return (getStorageUserInfo().role_id === 3);
      } catch (e) {
        return false;
      }
    }();

    Vue.prototype.isUserSystemGroup = function() {
      try {
        return (getStorageUserInfo().role_id < 11);
      } catch (e) {
        return false;
      }
    }();

    Vue.prototype.isUserAgentGroup = function() {
      try {
        return (getStorageUserInfo().role_id > 10);
      } catch (e) {
        return false;
      }
    }();
    //是否是代理管理员
    Vue.prototype.isUserAgentAdmin = function() {
      try {
        return (getStorageUserInfo().role_id === 11);
      } catch (e) {
        return true;
      }
    }();

    //是否是代理管理员
    Vue.prototype.isUserAgentStaff = function() {
      try {
        return (getStorageUserInfo().role_id === 16);
      } catch (e) {
        return true;
      }
    }();

    //管理员所有信息
    Vue.prototype.userInfo = getStorageUserInfo();
  },
};


